<template>
	<view class="transfer">
		<view class="transfer__loading">
			<view class="transfer__loading-icon"></view>
			<view class="transfer__loading-text">
				<template v-if="errMsgText">
					{{ errMsgText }}
				</template>
				<template v-else>
					页面重定向中...
				</template>
			</view>
		</view>
	</view>
</template>

<script>
	import { mapGetters } from 'vuex'
	import { buildUrl } from '@/utils/url.js'
	
	export default {
		data() {
			return {
				urlParams: null,
				errMsg: null
			}
		},
		computed: {
			...mapGetters([
				'isLogin'
			]),
			errMsgText () {
				if (!this.errMsg) {
					return
				}
				if (typeof this.errMsg !== 'object') {
					return this.errMsg
				}
				return JSON.stringify(this.errMsg)
			},
			urlParamsText () {
				if (!this.urlParams) {
					return
				}
				return JSON.stringify(this.urlParams, null, 4)
			}
		},
		onLoad (urlParams) {
			// urlParams.target
			// mineDataDay：日分析
			// mineDataWeek：周分析
			// mineDataMonth：月分析
			// 其他参数
			// 日分析： /pages/transfer/transfer?target=mineDataDay&startTime=xxxx&endTime=xxxx
			// 周分析： /pages/transfer/transfer?target=mineDataWeek&startTime=xxxx&endTime=xxxx
			// 月分析： /pages/transfer/transfer?target=mineDataMonth&startTime=xxxx&endTime=xxxx
			this.urlParams = urlParams || {}
			// this.urlParams = {
			// 	target: 'mineDataDay',
			// 	startTime: '2022-05-16',
			// 	endTime: '2022-05-16'
			// }
			let nextUrl = ''
			try {
				const { target, ...nextParams } = this.urlParams
				if (target === 'mineDataDay') {
					const { startTime, endTime, ...pageParams } = nextParams
					nextUrl = buildUrl('/packages/mine/mine-data-day/mine-data-day', Object.assign({}, pageParams, {
						countDate: startTime.split(' ')[0]
					}))
				} else if (target === 'mineDataWeek') {
					const { startTime, endTime, ...pageParams } = nextParams
					nextUrl = buildUrl('/packages/mine/mine-data-week/mine-data-week', Object.assign({}, pageParams, {
						startTime: startTime.split(' ')[0],
						endTime: endTime.split(' ')[0]
					}))
				} else if (target === 'mineDataMonth') {
					const { startTime, endTime, ...pageParams } = nextParams
					let startMonth = startTime.split(' ')[0]
					startMonth = startMonth.substr(0, startMonth.lastIndexOf('-'))
					nextUrl = buildUrl('/packages/mine/mine-data-month/mine-data-month', Object.assign({}, pageParams, {
						countMonth: startMonth
					}))
				}
			} catch (e) {
				this.errMsg = e
			}
			if (this.isLogin) {
				uni.reLaunch({
					url: nextUrl,
					fail: (err) => {
						this.errMsg = err
					}
				})
			} else {
				uni.reLaunch({
					url: buildUrl('/pages/login/login', {
						returnUrl: encodeURIComponent(nextUrl)
					}),
					fail: (err) => {
						this.errMsg = err
					}
				})
			}
		}
	}
</script>

<style lang="less">
.transfer__loading{
	position: absolute;
	top: 50%;
	left: 50%;
	transform: translate(-50%, -50%);
	color: #999;
}
</style>
